package com.kapukapu.authorization.config;

import com.kapukapu.authorization.service.UserDetailsServiceImpl;
import com.kapukapu.authorization.service.feign.IUserService;

import org.springframework.context.annotation.Bean;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;

/**
 * @author: sivan
 * @date: 2020/03/03
 * @email: zifanuu@gmail.com
 */
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
	private final IUserService iUserService;

	public WebSecurityConfig(IUserService iUserService) {
		this.iUserService = iUserService;
	}

	@Bean
	public PasswordEncoder passwordEncoder() {
		return new BCryptPasswordEncoder();
	}

	@Bean
	@Override
	public UserDetailsService userDetailsService() {
		return new UserDetailsServiceImpl(iUserService);
	}

	@Override
	protected void configure(HttpSecurity http) throws Exception {
		http.requestMatchers().antMatchers("/actuator/**").and().authorizeRequests()
				.antMatchers("/actuator/**").permitAll().and().requestMatchers()
				.anyRequest().and().authorizeRequests().anyRequest().authenticated().and()
				.formLogin().and().httpBasic();
	}
}
